home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / BSERROR.C < prev    next >
C/C++ Source or Header  |  1993-08-11  |  8KB  |  277 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    bserror.c
  5. //   Title:    Base library
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //
  24. //    This module contains error handling routines. There are two primary entry 
  25. //    points:
  26. //        Error() displays a message and returns FALSE.
  27. //        Fatal() displays a message and exits.
  28. //
  29. //    Both functions rely on DebugSetLocation() having been called. The error 
  30. //    functions can use an external function to display the message. 
  31. //
  32. //    The first paramters to the error functions can be either an error number
  33. //    (< 0xFFFF) or a pointer to a string. In either case, a variable number of 
  34. //    parameters may be passed. Normally, low-level error which occur very 
  35. //    infrequently use hard coded error strings while higher level messages
  36. //    use external messages.
  37. //
  38. //    Error messages are written to the program log file if the log level is
  39. //    >= 1 and file logging is enabled!
  40. //
  41. //    Error message text should not exceed 1024 bytes in length.
  42. //
  43. //    Error messages are display using a display function. A user supplied
  44. //    function may be registered, otherwise a default function is used.
  45. //
  46. //    External error display function should return non-zero if display failed.
  47. //
  48. //    The code in this module should be written entirely in C. 
  49. //    Do not use any C++ constructs.
  50. //
  51. //    This module is portable to:
  52. //        DOS 3.X+
  53. //        MS Windows 3.X+
  54. //        OS/2 2.X+
  55. //        OS/2 2.0 PM
  56. //        SCO UNIX.
  57. //
  58. //    The following compilers are supported:
  59. //        MSC 6.0A
  60. //        MSC/C++ 7.0
  61. //        Borland C++ 3.1 for DOS
  62. //        Borland C++ 1.0 for OS/2 2.X
  63. //        SCO UNIX cc
  64. //
  65. //----------------------------------------------------------------------------
  66. #include <bs.h>
  67.  
  68.  
  69. //----------------------------------------------------------------------------
  70. //    Prototypes
  71. //----------------------------------------------------------------------------
  72. static SHORT FN_E ErrorDisplay(PCSZ);
  73.  
  74.  
  75. //----------------------------------------------------------------------------
  76. //    Globals
  77. //----------------------------------------------------------------------------
  78. static PFNERR pfnerr = ErrorDisplay;
  79.  
  80.  
  81. //----------------------------------------------------------------------------
  82. //   Description:    
  83. //    Parameters:
  84. //       Returns:    FALSE.
  85. //----------------------------------------------------------------------------
  86. BOOL FN_EV Error_Debug(PCSZ psz, ...)
  87. {
  88.     va_list marker;
  89.     va_start(marker,psz);
  90.     ErrorV_Debug(psz, marker);
  91.     va_end(marker);
  92.     return FALSE;
  93. }
  94.  
  95.  
  96. //----------------------------------------------------------------------------
  97. //   Description:    Default error display function. Simply writes to standard
  98. //                          out using the base library output routines.
  99. //    Parameters:    pcsz        Message to display.
  100. //       Returns:    1
  101. //----------------------------------------------------------------------------
  102. static SHORT FN_E ErrorDisplay(PCSZ pcsz)
  103. {
  104. #if OS_WINDOWS
  105.     MessageBox(NULL, pcsz, "ERROR !", MB_ICONEXCLAMATION|MB_OK|MB_TASKMODAL);
  106. #else
  107.     OutputZ("\nERROR!\n");
  108.     OutputZ(pcsz);
  109.     OutputZ("\n\n");
  110. #endif
  111.     return 1;
  112. }
  113.  
  114.  
  115. //----------------------------------------------------------------------------
  116. //   Description:    
  117. //    Parameters:
  118. //       Returns:    FALSE.
  119. //----------------------------------------------------------------------------
  120. BOOL FN_EV ErrorNoMem_Debug(PCSZ pcsz, SIZET cLine)
  121. {
  122.     DebugSetLocation((PSZ)pcsz, cLine);
  123.     Error_Debug(
  124.         "Out of memory.\n"
  125.         "Please increase available system memory and try again.");
  126.     return FALSE;
  127. }
  128.  
  129.  
  130. //----------------------------------------------------------------------------
  131. //   Description:    Set the error display function.
  132. //                          This function should return non-zero if successful.
  133. //    Parameters:    pfnerr        New error display function.
  134. //                                        If null, the default function is installed.
  135. //       Returns:    TRUE if successful.
  136. //----------------------------------------------------------------------------
  137. BOOL FN_E ErrorSetDisplay(PFNERR _pfnerr)
  138. {
  139.     if (_pfnerr == NULL)
  140.         _pfnerr = ErrorDisplay;
  141.  
  142.     pfnerr = _pfnerr;
  143.     return TRUE;
  144. }
  145.  
  146.  
  147. //----------------------------------------------------------------------------
  148. //   Description:    Display an error message
  149. //                          Internal function used to format message text.
  150. //    Parameters:    pcsz                Error message text
  151. //                        marker            Variable length argument list
  152. //       Returns:    FALSE.
  153. //----------------------------------------------------------------------------
  154. BOOL FN_E ErrorV_Debug(PCSZ pcsz, va_list marker)
  155. {
  156. static BOOL afRecurse[MAX_THREADS];
  157.  
  158.     CHAR szFormat[IOBUF_SIZE];
  159.     PSZ pszOut;
  160.     USHORT us1, us2;
  161.     PSZ pszMsg = NULL;
  162.     BOOL fFormat = TRUE;
  163.  
  164.     if (afRecurse[THREADID - 1])            // Recursing!!!
  165.         return FALSE;
  166.  
  167.     afRecurse[THREADID - 1] = TRUE;
  168.  
  169.     DebugContext(szFormat, FALSE);        // Get context
  170.     pszOut = strchr(szFormat, '\0');        
  171.  
  172.     us1 = P2US1(pcsz);
  173.     us2 = P2US2(pcsz);
  174.     if (!us2)
  175.         {
  176.         CHAR szName[30];
  177.  
  178.         sprintf(szName, "ERROR%05u~STRING", us1);
  179.         if (!CfgFind(szName)
  180.         || !CfgRead(szName, (PBYTE _FAR_ *)&pszMsg, NULL))
  181.             {
  182.             sprintf(pszOut, "Unkown error # %u.", us1);
  183.             fFormat = FALSE;
  184.             }
  185.         else
  186.             pcsz = pszMsg;
  187.         }
  188.  
  189.     if (fFormat)                                // Format the message
  190.         vsprintf(pszOut, pcsz, marker);
  191.  
  192.     if (pszMsg)                                    // Free text buffer
  193.         MemFree(pszMsg);
  194.                                                     // Verify length
  195.     if (strlen(szFormat) >= sizeof(szFormat))
  196.         Halt("Fatal internal buffer overflow.\nPlease contact customer service.");
  197.  
  198.     if ((*pfnerr)(szFormat) == 0)            // Display
  199.         ErrorDisplay(szFormat);
  200.  
  201.     Log_DebugV(1, pcsz, marker);            // Write to log file
  202.  
  203.     afRecurse[THREADID - 1] = FALSE;
  204.     return FALSE;
  205. }
  206.  
  207.  
  208.  
  209. //----------------------------------------------------------------------------
  210. //   Description:    
  211. //    Parameters:
  212. //       Returns:    FALSE.
  213. //----------------------------------------------------------------------------
  214. BOOL FN_EV Fatal_Debug(PCSZ psz, ...)
  215. {
  216.     va_list marker;
  217.     va_start(marker,psz);
  218.     ErrorV_Debug(psz, marker);
  219.     va_end(marker);
  220.     exit(99);
  221.     return FALSE;
  222. }
  223.  
  224.  
  225. //----------------------------------------------------------------------------
  226. //   Description:    Invalid function call message.
  227. //    Parameters:    pcsz        Function name
  228. //       Returns:    FALSE
  229. //----------------------------------------------------------------------------
  230. BOOL FN_E Invalid_Debug(PCSZ pcsz)
  231. {
  232.     CHAR szBuffer[160];
  233.     sprintf(szBuffer, "Invalid or unimplemented function call - '%s'.", pcsz);
  234.     Fatal_Debug(szBuffer);
  235.     return FALSE;
  236. }
  237.  
  238.  
  239. //----------------------------------------------------------------------------
  240. //   Description:    Functionality not implemented
  241. //    Parameters:    pcsz        Function name
  242. //       Returns:    FALSE
  243. //----------------------------------------------------------------------------
  244. BOOL FN_E NotImplemented_Debug(PCSZ pcsz)
  245. {
  246.     CHAR szBuffer[160];
  247.     if (pcsz == NULL)
  248.         pcsz = "I'm sorry, that";
  249.     sprintf(szBuffer, "%s is not implemented.", pcsz);
  250.     Error_Debug(szBuffer);
  251.     return FALSE;
  252. }
  253.  
  254.  
  255. //----------------------------------------------------------------------------
  256. //   Description:    Run standard test suite
  257. //    Parameters:    sTest        Test to run.
  258. //                                        0        Run all default tests (except).
  259. //       Returns:    TRUE if successful.
  260. //----------------------------------------------------------------------------
  261. #if COMPILE_TEST
  262. BOOL FN ErrorTest(SHORT sTest)
  263. {
  264.     Error((PSZ)1);
  265.     Error((PSZ)0xFFFF);                        // Non-existing error number
  266.     Error("This is a %s error", "normal");
  267.  
  268.     if (sTest == 1)
  269.         Fatal("This is a %s error", "fatal");
  270.  
  271.     return TRUE;
  272. }
  273. #endif
  274. //----------------------------------------------------------------------------
  275. //------------------------------- End of File --------------------------------
  276. //----------------------------------------------------------------------------
  277.